package com.lingqi.commands;

import com.lingqi.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

// 借书操作
public class BorrowBookCommand {
    public static void main(String[] args) throws SQLException {
        // 1.进行登陆操作
        Scanner scanner = new Scanner(System.in);
        System.out.print("登录-请输入用户名：");
        String userName = scanner.nextLine();
        System.out.println("DEBUG:用户输入的用户名 = " + userName);

        int rid;
        try(Connection c = DBUtil.connection()){
            String sql = "select rid from readers where name = ?";
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setString(1,userName);

                System.out.println("DEBUG:执行的sql = " + ps);
                try(ResultSet rs = ps.executeQuery()){
                    if(!rs.next()){
                        System.out.println("登陆失败");
                        return;
                    }
                    rid = rs.getInt("rid");
                }
            }
        }
        System.out.println("DEBUG:查询到的 rid = " + rid);

        // 当前用户（系统目前登录用户信息）用 rid 记录
        System.out.println("登陆成功");
        System.out.print("请输入书名：");
        String bookName = scanner.nextLine();
        System.out.println("DEBUG:用户输入的书名 = " + bookName);

        // 借书的前期：1）书名存在 2）书的count > 0
        int bid;
        int count;
        try(Connection c = DBUtil.connection()){
            String sql = "select bid, count from books where name = ?";
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setString(1,bookName);
                System.out.println("DEBUG:执行的sql = " + ps);

                try(ResultSet rs = ps.executeQuery()){
                    if(!rs.next()){
                        System.out.println("查无此书");
                        return;
                    }

                    bid = rs.getInt("bid");
                    count = rs.getInt("count");
                }
            }
        }
        System.out.println("DEBUG:查询到的 bid = " + bid);
        System.out.println("DEBUG:查询到的 count = " + count);

        // 书存在
        if(count == 0){
            // 我们的书的count 理论不会 < 0
            System.out.println("书已借空");
            return;
        }

        // 书存在 && count > 0
        // 1)修改书的count = count - 1
        // 2)向借阅记录中插入一条借阅记录
        try(Connection c = DBUtil.connection()){
            String sql1 = "update books set count = count - 1 where bid = ?";
            String sql2 = "insert into records (rid,bid) values (?,?)";

            try(PreparedStatement ps = c.prepareStatement(sql1)){
                ps.setInt(1,bid);
                System.out.println("DEBUG:执行的sql = " + ps);

                ps.executeUpdate();
            }
            try(PreparedStatement ps = c.prepareStatement(sql2)){
                ps.setInt(1,rid);
                ps.setInt(2,bid);
                System.out.println("DEBUG:执行的sql = " + ps);

                ps.executeUpdate();
            }
        }
        System.out.println("借书完成");
    }
}
